﻿using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using JinJun.YShow.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace JinJun.YShow.Web.Controllers
{
    public class CategoryController : BaseController
    {
        /*
          只定义了当前表的业务层，如需其他自行添加构造接口
             */

        private ICategoryService _categoryService;

        public CategoryController(ICategoryService categoryService)
        {
            _categoryService = categoryService;
        }

        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetCategoryList(int page,int limit)
        {
            //定义表数据总数变量
            int count = 0;
            Expression<Func<Category, bool>> lambdaCategory = a => true;
            Expression<Func<Category, int>> orderCategory = a =>a.Id;
            //创建List集合接收业务层返回值
            List<Category> categories = _categoryService.QueryPageList(page, limit, lambdaCategory, orderCategory,out count);
            //创建PageListResult对象
            PageListResult<Category> pageListResult = new PageListResult<Category>();
            //框架属性赋值
            pageListResult.code = 0;
            pageListResult.count = count;
            pageListResult.msg = "";
            pageListResult.data = categories;
            //返回PageListResult对象
            return Json(pageListResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public JsonResult DeleteCategory(Category category)
        {
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _categoryService.DeleteCategory(category);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 根据层级获取对应分类列表
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public JsonResult GetParent(int LevelId)
        {
            List<Category> categories = new List<Category>();
            Expression<Func<Category, bool>> lambdaCategory = a => a.CategoryLevel== LevelId;
            categories = _categoryService.QueryList(lambdaCategory);
            return Json(categories, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }


        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public JsonResult AddCategory(Category category)
        {
            category.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _categoryService.Add(category);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 编辑分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public JsonResult EditCategory(Category category)
        {
            category.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _categoryService.Update(category);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }
    }
}